using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace CCEmployeeSyncService
{
	public class CCEmployeeSyncService : System.ServiceProcess.ServiceBase
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private System.Diagnostics.EventLog eventLog1 = null;

		public CCEmployeeSyncService()
		{
			// This call is required by the Windows.Forms Component Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitComponent call
			if (!System.Diagnostics.EventLog.SourceExists("CCEmployeeSyncService"))
			{
				System.Diagnostics.EventLog.CreateEventSource("CCEmployeeSyncService", "Application");
			}
			eventLog1.Source = "CCEmployeeSyncService";
			eventLog1.Log = "Application";
		}

		// The main entry point for the process
		static void Main()
		{
			System.ServiceProcess.ServiceBase[] ServicesToRun;
	
			// More than one user Service may run within the same process. To add
			// another service to this process, change the following line to
			// create a second service object. For example,
			//
			//   ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
			//
			ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CCEmployeeSyncService() };

			System.ServiceProcess.ServiceBase.Run(ServicesToRun);
		}

		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.eventLog1 = new System.Diagnostics.EventLog();
			((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
			// 
			// CCEmployeeSyncService
			// 
			this.ServiceName = "ASBCCEmployeeSyncService";
			((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}


		private static bool flag = true;

		/// <summary>
		/// Set things in motion so your service can do its work.
		/// </summary>
		protected override void OnStart(string[] args)
		{
			//Thread.Sleep(30000);
			eventLog1.WriteEntry("CCEmployeeSyncService is now in OnStart.");

			BeginSync();
		}
 
		/// <summary>
		/// Stop this service.
		/// </summary>
		protected override void OnStop()
		{
			flag = false;
			
			// TODO: Add code here to perform any tear-down necessary to stop your service.
			eventLog1.WriteEntry("CCEmployeeSyncService is now in OnStop.");
		}

		private void BeginSync()
		{
			System.Threading.Thread mailThread = new System.Threading.Thread( new System.Threading.ThreadStart(SyncEmployee));
			mailThread.Start();			
		}

		private static int _interval = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["Interval"]);

		private string _remindTime = System.Configuration.ConfigurationSettings.AppSettings["RemindTime"].ToString();

		private void SyncEmployee()
		{
			int sleepTime = _interval * 1000 ;

			ISync empService = BEGDC.Utility.Castle.AppContainer.Instance["EmployeeSync"] as  ISync;

			while(flag)
			{

				try
				{
					if (System.DateTime.Now.ToString("HH:mm") == _remindTime)
					{
						//TODO
						System.DateTime dtStart = System.DateTime.Now;

						empService.Start();

						System.DateTime dtEnd = System.DateTime.Now;

						System.TimeSpan ts = dtEnd - dtStart;

						eventLog1.WriteEntry("Successfully. Use time:" + ts.ToString());

					}

				}
				catch (Exception ex)
				{
					this.eventLog1.WriteEntry(ex.Message);
					System.Threading.Thread.Sleep( sleepTime * 10 );
				}
				System.Threading.Thread.Sleep( sleepTime );
			}
			System.Threading.Thread.CurrentThread.Abort();
		}
	}
}
